<?php

class Admin_Model_Roleprivilege extends Zend_Db_Table_Abstract{
    protected $_name = 'role_privilege';
    public function getDataByRoleId($roleId)
    {
        $query = $this->select()
                    ->from($this->_name)
                    ->where('roleId = ?', (int)$roleId);
        return $this->fetchRow($query);
    }
    public function getDataByUserId($userId)
    {
        $query = $this->select()
                    ->setIntegrityCheck(false)
                    ->from(array('r'=>$this->_name), array('r.roleId', 'r.resourceId', 'r.gameId'))
                    ->where('r.roleId IN (SELECT u.roleId FROM user_role u WHERE u.userId = '.(int)$userId.')');
        return $this->fetchAll($query);
    }
    public function addData($roleId, $resourceId = '', $gameId = '')
    {
        $data = array('roleId' => $roleId, 'resourceId'=> $resourceId, 'gameId'=> $gameId);
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function deleteData($roleId)
    {
        $this->delete('roleId = '.(int) $roleId);
    }
    public function updateData($roleId, $resourceId = '', $gameId = '')
    {
        $data = array('resourceId'=> $resourceId, 'gameId'=> $gameId);
        return $this->update($data, 'roleId = '.$roleId);
    }
}